package login;

import io.ClientSession;
import io.OperationResponse;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;

public class TSZlogin {
	public static void Tszlogon(OperationResponse operationRequest,
			ClientSession session) {
		try {

			if (operationRequest.Parameters.size() < 2) {
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, null, ReturnCode.No, "");
				session.SendOperationResponse(response);
			}
			String id = (String) operationRequest.Parameters
					.get(ParameterType.UserID);
			String token = (String) operationRequest.Parameters
					.get(ParameterType.UserPwd);
			// String databeas = (String) operationRequest.P
			String deviceID = (String) operationRequest.Parameters
					.get(ParameterType.DeviceID);
			// .get(ParameterType.DataBeas);
			// String table = (String) operationRequest.Parameters
			// .get(ParameterType.TableName);
			String isGetIP = (String) operationRequest.Parameters
					.get(ParameterType.IsGetIP);
			String PhoneType=(String) operationRequest.Parameters
					.get(ParameterType.PhoneType);

			if (id.equals("zealm") && token.equals("")) {
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, AppConfig.dicAppInfo,
						ReturnCode.GetServer, "");
				session.SendOperationResponse(response);
				return;

			}
			String dicID = GetLoginTSZ(id);
			if (dicID.equals("")) {
				return;
			}

			id = "tsz" + dicID;
			operationRequest.Parameters.put(ParameterType.UserID, id);
			HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
			myPrams.put(ParameterType.UserID, id);
			OperationResponse responseID = new OperationResponse(
					operationRequest.OperationCode, myPrams, ReturnCode.HasID,
					"");
			session.SendOperationResponse(responseID);
			Util.sdkReceived(operationRequest, session, id, deviceID, isGetIP,"qh",PhoneType,"");
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	static String tszServerUrl = "https://openapi.360.cn/oauth2/access_token";
	static String tszServerPlayer = "https://openapi.360.cn/user/me.json";
	static int tszApp_id = 201320076;
	static String tszAppKey = "5f6853e8c77fe7c0ee9811dffbd09521";
	static String tszAppSecret = "c130281f52768eccba48488713752f09";

	public static String GetLoginTSZ(String mCode)
			throws ClientProtocolException, IOException, JSONException {
		if (mCode.isEmpty()) {
			return null;
		}

		String body = "access_token=" + mCode;
		body = new String(body.getBytes("gbk"), "utf-8");
		String result = HttpClient.sendXMLDataByPost(tszServerPlayer, body);// httppost鏂瑰紡璋冪敤鏈嶅姟鍣ㄦ帴鍙�
		JSONObject date = new JSONObject(result);
		String id = date.optString("id");
		if (!id.isEmpty()) {
			return id;
		}
		return null;

	}

}
